package companyTest.huawei;

import java.util.*;

public class Code1 {
    public static void main(String[] args) {
        // please define the JAVA input here. For example: Scanner s = new Scanner(System.in);
        // please finish the function body here.
        // please define the JAVA output here. For example: System.out.println(s.nextInt());
        Scanner s=new Scanner(System.in);
        int N=s.nextInt();
        Set<Integer> []sets=new Set[N];
        for (int i = 0; i < N; i++) {
            sets[i]=new HashSet<>();
        }
        int []inDegree=new int[N];
        Queue<Integer>queue=new ArrayDeque<>();
        for (int i = 0; i < N; i++) {
            Set<Integer>set=sets[i];
            int n=s.nextInt();
            if (n==0)queue.add(i);
            inDegree[i]=n;
            for (int j = 0; j < n; j++) {
                set.add(s.nextInt()-1);
            }
        }
        int ans=0;
        int sum=0;
        while (!queue.isEmpty()){
            int size=queue.size();
            sum+=size;
            ans++;
            for (int i = 0; i < size; i++) {
                int index=queue.poll();
                for (int j = 0; j < N; j++) {
                    if (sets[j].contains(index)){
                        sets[j].remove(index);
                        inDegree[j]--;
                        if (inDegree[j]==0)queue.offer(j);
                    }
                }
            }
        }
        System.out.println(ans);
        if (sum!=N) System.out.println(-1);
        else System.out.println(ans);
    }
}
